package com.example.desarollo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AllLibros extends ListActivity {

	// Progress Dialog
		private ProgressDialog pDialog;

		// Creating JSON Parser object
		Httppostaux jParser = new Httppostaux();
		//JSONParser jParser = new JSONParser();

		ArrayList<HashMap<String, String>> librosList;

		private static String url_all_libros = "http://186.4.149.247/desarrollo/busqueda2.php";
		
		// JSON Node names
		private static final String TAG_SUCCESS = "success";
		private static final String TAG_libros = "libros";
		private static final String TAG_fecha = "fecha";
		private static final String TAG_ubicacion = "ubicacion";
		
		
		JSONArray libros = null;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.all_libros);

			// Hashmap for ListView
			librosList = new ArrayList<HashMap<String, String>>();

			// Loading empleados in Background Thread
			
			new LoadAlllibros().execute();
			
			
		
			// Get listview
			ListView lv = getListView();
			
			
			
			
		}
			
			class LoadAlllibros extends AsyncTask<String, String, String> {

				/**
				 * Before starting background thread Show Progress Dialog
				 * */
				@Override
				protected void onPreExecute() {
					super.onPreExecute();
					pDialog = new ProgressDialog(AllLibros.this);
					pDialog.setMessage("Cargando libros. Please wait...");
					pDialog.setIndeterminate(false);
					pDialog.setCancelable(false);
					pDialog.show();
				}

				/**
				 * getting All empleados from url
				 * */
				protected String doInBackground(String... args) {
					// Building Parameters
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					// getting JSON string from URL
					JSONObject json = jParser.makeHttpRequest(url_all_libros, "GET", params);
					
					// Check your log cat for JSON reponse
					Log.d("All libros: ", json.toString());

					try {
						// Checking for SUCCESS TAG
						int success = json.getInt(TAG_SUCCESS);

						if (success == 1) {
							// empleados found
							// Getting Array of empleados
							libros = json.getJSONArray(TAG_libros);

							// looping through All empleados
							for (int i = 0; i < libros.length(); i++) {
								JSONObject c = libros.getJSONObject(i);

								// Storing each json item in variable
								String fecha = c.getString(TAG_fecha);
								String ubicacion = c.getString(TAG_ubicacion);
								

								// creating new HashMap
								HashMap<String, String> map = new HashMap<String, String>();

								// adding each child node to HashMap key => value
								map.put(TAG_fecha, fecha);
								map.put(TAG_ubicacion, ubicacion);
								

								// adding HashList to ArrayList
								librosList.add(map);
							}
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}

					return null;
				}

				/**
				 * After completing background task Dismiss the progress dialog
				 * **/
				protected void onPostExecute(String file_url) {
					// dismiss the dialog after getting all empleados
					pDialog.dismiss();
					// updating UI from Background Thread
					runOnUiThread(new Runnable() {
						public void run() {
							/**
							 * Updating parsed JSON data into ListView
							 * */
							ListAdapter adapter = new SimpleAdapter(
									AllLibros.this, librosList,
									R.layout.list_item, new String[] { TAG_fecha,
											TAG_fecha },
									new int[] { R.id.fecha, R.id.ubicacion });
							// updating listview
							setListAdapter(adapter);
						}
					});

				}

			}
					
		}
